﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace myMoney.File
{
    public partial class ucMain : UserControl
    {
        public ucMain()
        {
            InitializeComponent();
            loadData();
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            this.pBox1.Location = new Point(this.Width - 10 - this.pBox1.Width, this.pBox1.Location.Y);
        }

        public void loadData()
        {
            DataTable dt = MoneyClass.User.get(Main.CurrentGuid);
            if (dt.Rows.Count <= 0)
            {
                MessageBox.Show("系统错误");
                return;
            }
            this.lblUser.Text = string.Format("{0}({1}:{2})", dt.Rows[0]["userid"].ToString(), dt.Rows[0]["username"].ToString(), dt.Rows[0]["comment"].ToString());
            dt.Dispose();

            this.lblYear.Text = string.Format("本年({0}年)", DateTime.Today.Year);
            this.lblYearIncome.Text = string.Format("{0}元", MoneyClass.Expenses.getCount(Main.CurrentGuid, "0", DateTime.Now.ToString("yyyy-01-01"), DateTime.Now.AddDays(1).ToString("yyyy-MM-dd")).ToString("N2"));
            this.lblYearExpenditure.Text = string.Format("{0}元", MoneyClass.Expenses.getCount(Main.CurrentGuid, "1", DateTime.Now.ToString("yyyy-01-01"), DateTime.Now.AddDays(1).ToString("yyyy-MM-dd")).ToString("N2"));

            this.lblMonth.Text = string.Format("本月({0}月)", DateTime.Today.Month);
            this.lblMonthIncome.Text = string.Format("{0}元", MoneyClass.Expenses.getCount(Main.CurrentGuid, "0", DateTime.Now.ToString("yyyy-MM-01"), DateTime.Now.AddDays(1).ToString("yyyy-MM-dd")).ToString("N2"));
            this.lblMonthExpenditure.Text = string.Format("{0}元", MoneyClass.Expenses.getCount(Main.CurrentGuid, "1", DateTime.Now.ToString("yyyy-MM-01"), DateTime.Now.AddDays(1).ToString("yyyy-MM-dd")).ToString("N2"));

            this.lblDay.Text = string.Format("本日({0}日)", DateTime.Today.ToString("dd"));
            this.lblDayIncome.Text = string.Format("{0}元", MoneyClass.Expenses.getCount(Main.CurrentGuid, "0", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.AddDays(1).ToString("yyyy-MM-dd")).ToString("N2"));
            this.lblDayExpenditure.Text = string.Format("{0}元", MoneyClass.Expenses.getCount(Main.CurrentGuid, "1", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.AddDays(1).ToString("yyyy-MM-dd")).ToString("N2"));

            MoneyClass.ExpensesPeriod myPeriod = MoneyClass.Expenses.getPeriod(Main.CurrentGuid);
            this.lblTimePeriod.Text = string.Format("{0} - {1}", myPeriod.Start, myPeriod.End);
            this.lblAllIncome.Text = string.Format("{0}元", MoneyClass.Expenses.getCount(Main.CurrentGuid, "0").ToString("N2"));
            this.lblAllExpenditure.Text = string.Format("{0}元", MoneyClass.Expenses.getCount(Main.CurrentGuid, "1").ToString("N2"));
        }

        private void pbYearIncome_Click(object sender, EventArgs e)
        {
            Finance.Record myRecord = new Finance.Record();
            myRecord.type = "0";
            myRecord.start = DateTime.Parse(DateTime.Now.ToString("yyyy-01-01"));
            myRecord.end = DateTime.Now;
            myRecord.ShowDialog();
            myRecord.Dispose();
        }

        private void pbYearExpenditure_Click(object sender, EventArgs e)
        {
            Finance.Record myRecord = new Finance.Record();
            myRecord.type = "1";
            myRecord.start = DateTime.Parse(DateTime.Now.ToString("yyyy-01-01"));
            myRecord.end = DateTime.Now;
            myRecord.ShowDialog();
            myRecord.Dispose();
        }

        private void pbMonthIncome_Click(object sender, EventArgs e)
        {
            Finance.Record myRecord = new Finance.Record();
            myRecord.type = "0";
            myRecord.start = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01"));
            myRecord.end = DateTime.Now;
            myRecord.ShowDialog();
            myRecord.Dispose();
        }

        private void pbMonthExpenditure_Click(object sender, EventArgs e)
        {
            Finance.Record myRecord = new Finance.Record();
            myRecord.type = "1";
            myRecord.start = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01"));
            myRecord.end = DateTime.Now;
            myRecord.ShowDialog();
            myRecord.Dispose();
        }

        private void pbDayIncome_Click(object sender, EventArgs e)
        {
            Finance.Record myRecord = new Finance.Record();
            myRecord.type = "0";
            myRecord.start = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
            myRecord.end = DateTime.Now;
            myRecord.ShowDialog();
            myRecord.Dispose();
        }

        private void pbDayExpenditure_Click(object sender, EventArgs e)
        {
            Finance.Record myRecord = new Finance.Record();
            myRecord.type = "1";
            myRecord.start = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
            myRecord.end = DateTime.Now;
            myRecord.ShowDialog();
            myRecord.Dispose();
        }
    }
}
